<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form"  @submit="handleSubmit">
        <a-form-item label="奖励百分比" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-input type="number" addon-after="%" placeholder="奖励百分比，例15即15%" v-decorator="['award', { rules: [{ required: true, message: '奖励百分比，例15即15%' }] }]"/>
        </a-form-item>
        <a-form-item label="业绩金额" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-input type="number" placeholder="设置需要的业绩金额" v-decorator="['performance', { rules: [{ required: true, message: '设置需要的业绩金额' }] }]"/>
        </a-form-item>
        <a-form-item label="描述" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-textarea placeholder="请输入描述内容..." :rows="6" v-decorator="['des', { rules: [{ required: false, message: '描述...' }] }]"/>
        </a-form-item>
        <a-form-item :wrapper-col="{ span: wrapperCol.span, offset: labelCol.span }">
          <a-button type="primary" html-type="submit">提交</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/market/wine/perform'

export default {
  components: {
  },
  data () {
    return {
      // 当前设置项的key
      key: 'perform',
      // 标签布局属性
      labelCol: { span: 3 },
      // 输入框布局属性
      wrapperCol: { span: 7 },
      // loading状态
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录详情
      record: {}
    }
  },
  // 初始化数据
  created () {
    // 获取当前详情记录
    // this.getDetail()
  },
  methods: {

    // 获取当前详情记录
    getDetail () {
      this.isLoading = true
      Api.detail()
        .then(result => {
          // 当前记录
          this.record = result.data.values
          // 设置默认值
          this.setFieldsValue()
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, $nextTick, form: { setFieldsValue } } = this
      $nextTick(() => {
        setFieldsValue(pick(record, ['award', 'performance', 'des']))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.isLoading = true
      Api.add({ form: values })
        .then((result) => {
          // 显示提示信息
          this.$message.success(result.message, 1.5,()=>{
            this.$router.push('/market/wine/performindex')
          })
        })
        .finally((result) => {
          this.isLoading = false
        })
    }

  }
}
</script>
<style lang="less" scoped>
.ant-form-item {
  .ant-form-item {
    margin-bottom: 0;
  }
}

/deep/.ant-form-item-control {
  padding-left: 10px;
  .ant-form-item-control {
    padding-left: 0;
  }
}
</style>